﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Snake.Scripts
{
    class Map:Singleton<Map>
    {
        private const int mapWidth = 30;
        private const int mapHeight = 30;

        private EPointType[,] map = null;

        private Random random = new Random();

        public void Start()
        {
            map = new EPointType[mapWidth, mapHeight];
            int width = map.GetLength(0);
            int height = map.GetLength(1);
            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    if (i == 0 || i == width-1)
                    {
                        map[i, j] = EPointType.Wall;
                    }
                    else if (j == 0 || j == height-1)
                    {
                        map[i, j] = EPointType.Wall;
                    }
                    else
                    {
                        map[i, j] = EPointType.Blank;
                    }
                }
            }

            GenFood();

        }

        public void Draw()
        {
            for (int i = 0; i < map.GetLength(0); i++)
            {
                for (int j = 0; j < map.GetLength(1); j++)
                {
                    Console.SetCursorPosition(i*2, j);

                    switch (map[i,j])
                    {
                        case EPointType.Blank:
                            Console.Write(" ");
                            break;
                        case EPointType.Wall:
                            Console.ForegroundColor = ConsoleColor.Yellow;
                            Console.Write("*");
                            break;
                        case EPointType.Food:
                            Console.Write("+");
                            break;
                        default:
                            break;
                    }
                }
            }
        }

        public void GenFood()
        {
            for (; ; )
            {
                Point pt = new Point(random.Next(mapWidth), random.Next(mapHeight));
                if(GetAt(pt) == EPointType.Blank)
                {
                    SetAt(pt, EPointType.Food);
                    return;
                }
            }

        }

        public EPointType GetAt(Point at)
        {
            return map[at.x, at.y];
        }

        public void SetAt(Point at, EPointType val)
        {
            map[at.x, at.y] = val;
        }
    }
}
